﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;


namespace Room
{
    public class PlayerAIStateFishing : PlayerAIStateBase
    {
        protected override void OnEnter()
        {
          
            if (!loadedFishAni)
            {
                controller.owner.StartCoroutine(_LoadFishAni());
            }
            else
            {
                controller.owner.PlayAnim(AniName.DIAO_YU,WrapMode.Loop);
            }

            var Actor = controller as RCAIActor;
            fishingRod = Actor.fishObj;

            if (controller.owner.lod.Model != null)
            {
                if (fishingRod == null)
                {
                    controller.owner.StartCoroutine(_LoadFishModel());
                }
                else
                {
                    fishingRod.SetActive(true);
                }

                if(fish == null)
                {
                    controller.owner.StartCoroutine(_Fish());
                }
                else
                {
                    controller.owner.StartCoroutine(_FishLoaded());
                }

                FaceToFish();
            }

        }

        protected override void OnLeave()
        {
            if (!loadedFishAni)
            {
                controller.owner.StopCoroutine("_LoadFishAni");
            }
           
            controller.owner.PlayAnim(AniName.STAND);
            if (controller.owner.lod.Model != null)
            {
                if (fishingRod != null)
                {
                    fishingRod.SetActive(false);
                    var resName = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.FishingRod;
                    
                    ResourceLoader.GetInstance().weaponLoader.UnloadWeapon(fishingRod, resName);
                    var Actor = controller as RCAIActor;
                    Actor.fishObj = null;
                }
                else
                {
                    controller.owner.StopCoroutine("_LoadFishModel");
                }

                if(fish!=null)
                {
                    fish.SetActive(false);
                    var resName = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.FishModel;
                    ResourceLoader.GetInstance().weaponLoader.UnloadWeapon(fish, resName);
                }
            }
        }


        void FaceToFish()
        {
            if (long.Parse(controller.owner.name) == StaticData.playerData.playerId)
            {
                var fish = Room.RoomManager.currentRoom.charactorManager.GetCharactor(StaticData.resId + "@" + StaticData.resPosId);
                controller.owner.transform.LookAt(fish.transform);
            }

        }

        bool loadedFishAni = false;

        IEnumerator _LoadFishAni()
        {
            if (controller.owner.lod.Model != null)
            {
                var resourceName = controller.owner.lod.Model.name;

                yield return AssetLoad.AssetBundleManager.Instance.StartCoroutine(
                ResourceLoader.GetInstance().modelLoader.LoadModelExtend(resourceName + ConstData.extendResFieldPostfix,
                    (asset) =>
                    {
                        if (asset != null)
                        {
                            var Assets = asset as AnimClipsAsset;
                            if (Assets != null && Assets.clips.Count > 0)
                            {
                                var ac = Assets.clips[0];
                                if (ac != null)
                                {
                                    foreach (var ass in Assets.clips)
                                    {
                                        if (ass.name == AniName.DIAO_YU)
                                        {
                                            ac = ass;
                                        }
                                    }
                                    controller.owner.lod.Model.GetComponent<ModelRenderKit>().AddLifeAni(ac);
                                    controller.owner.PlayAnim(AniName.DIAO_YU);

                                    loadedFishAni = true;
                                }
                            }
                        }
                    }
                    )
                );
            }
        }

        GameObject fishingRod;

        IEnumerator _LoadFishModel()
        {
            if (controller.owner.lod.Model != null)
            {
                var tran = controller.owner.lod.Model.GetComponent<ModelRenderKit>().GetTransform("Bip001 R Hand");

                if (fishingRod == null)
                {
                    var fishingRodName = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.FishingRod;
                    yield return AssetLoad.AssetBundleManager.Instance.StartCoroutine(
                       ResourceLoader.GetInstance().weaponLoader.LoadWeapon(fishingRodName, (asset) =>
                       {
                           fishingRod = asset as GameObject;
                           if (fishingRod != null)
                           {
                               fishingRod.transform.SetParent(tran);
                               fishingRod.transform.localPosition = new Vector3(-0.05f, 0.025f, -0.224f);
                               fishingRod.transform.localScale = Vector3.one;
                               fishingRod.transform.localRotation = Quaternion.Euler(new Vector3(-2.173f, -7.8f, -99f + 180f));
                               fishingRod.SetActive(true);

                               var Actor = controller as RCAIActor;
                               Actor.fishObj = fishingRod;

                           }

                       }));

                }
            }
        }

        GameObject fish;
        IEnumerator _Fish()
        {
            if (fish == null)
            {
                var tran = controller.owner.lod.Model.GetComponent<ModelRenderKit>().GetTransform("Bip001 R Hand");

                var fishName = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.FishModel;
                yield return AssetLoad.AssetBundleManager.Instance.StartCoroutine(
                   ResourceLoader.GetInstance().weaponLoader.LoadWeapon(fishName, (asset) =>
                   {
                       fish = asset as GameObject;
                       if (fish != null)
                       {
                           fish.transform.SetParent(tran);
                           fish.transform.localPosition = new Vector3(-0.05f, 0.025f, -0.224f);
                           fish.transform.localScale = Vector3.one;
                           fish.transform.localRotation = Quaternion.Euler(new Vector3(-2.173f, -7.8f, -99f + 180f));

                           fish.SetActive(false);
                          
                       }

                   }));
                yield return new WaitForSeconds(260f / 30f);
                if (fish != null)
                    fish.SetActive(true);
            }
        }

        IEnumerator _FishLoaded()
        {
            yield return new WaitForSeconds(260f / 30f);
            if (fish != null)
                fish.SetActive(true);
        }

    }
}